<?
if( !defined( 'MODULE_FILE' ) ) {
	die( "cant access file directly!" );
}

autoLoader();

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );
$emp_id = ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) && true == valStr( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : NULL;

$strAction = ( ( true == isset( $_GET['action'] ) && true == valStr( $_GET['action'] ) ) ? $_GET['action'] : NULL );

$objPatient 	= new Patients();
$intPatientId 	= NULL;

if( true == isset( $_GET['patient_id'] ) && true == valStr( $_GET['patient_id'] ) ) {
	$objPatient = $objPatient->fetchPatientsById( $_GET['patient_id'] );
	
	if( true == valObj( $objPatient, 'BasePatients' ) ) {
		$intPatientId = $objPatient->getId();
	}
}

?>
<html>
<head>
<?php require_once 'commonJS.php'; ?>

	<link rel="stylesheet" type="text/css" media="screen" href="js/Autocomplete/resources/css/smoothness/jquery-ui-1.10.1.custom.css"/>
	<link rel="stylesheet" type="text/css" media="screen" href="js/Autocomplete/resources/css/smoothness/jquery.ui.combogrid.css"/>
	<link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
	
	<script type="text/javascript" src="js/Autocomplete/resources/jquery/jquery-ui-1.10.1.custom.min.js"></script>
	<script type="text/javascript" src="js/Autocomplete/resources/plugin/jquery.ui.combogrid-1.6.3.js"></script>
	<script src="js/plugins/dataTables/jquery.dataTables.js"></script>
	<script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>

</head>
<body>
<br>
<div class="col-lg-10">
	<div class="panel panel-primary">
		<div class="panel-heading"><i class="fa fa-bell fa-fw"></i>Yellow Pages.</div>
		<div class="panel-body">
		<!-- action="modules.php?file=insert_opd_case_paper&type=insert&action=insert&mid=<? //echo $mid; ?>&smid=<? //echo $smid; ?>" -->
			<form METHOD="POST" action="#" id="patientAdmission" name="patientAdmission" >
				<div>
					<div style="float: left;">
						Patient Details:- <input type="text" name="patient_details" id="patient_details" value="<?php echo $intPatientId; ?>" class="validate[required]" placeholder="ID or Patient name.." style="width:400px;"/>
						<input type="hidden" name="patient_id" id="patient_id" value="<?php echo $intPatientId; ?>" />
						<input type="hidden" name="case_paper_id" id="case_paper_id" value="<?php echo $intCasePaperId; ?>" />
						<input type="hidden" name="action" id="action" value="<?php echo $strAction; ?>" />
						<input type="button" name="search_patient" class="btn btn-danger" value="SEARCH"  onclick="searchPatient()" />
						&nbsp;&nbsp;&nbsp;
					</div>
					<div style="display: <?php echo ( true == valStr( $strName ) ) ? 'block;' : 'none;'; ?>" id="success_display">
						<img alt="Patient Found with info.." src="images/green_tick.gif" width="40" height="40">
						<label id='disp_patient_name'><?php echo '(' . $intPatientId . ') ' . $strName; ?></label>
					</div>
					<div style="display: none;" id="failed_display">
						<img alt="Patient Found with info.." src="images/delete.png" width="40" height="40">
						<label id='disp_failed'></label>
					</div>
				</div>
				<div style="clear: both;"></div>
				<div id="patient_info"></div>
				<hr style="margin-top: 0px; margin-bottom: 0px;" />
		
		<!--*******************************************************/ -->
				<div id="view_yellow_pages"></div>
				
				<input type="hidden" name="mid" id="mid" value="<?php echo $mid; ?>" />
				<input type="hidden" name="smid" id="smid" value="<?php echo $smid; ?>" />
			</form>
		</div>
	</div>
</div>	
</body>
<script>
	$(document).ready(function(){
		$( "#from" ).datepicker({
			dateFormat: 'dd-mm-yy',
			showOn: "button",
			buttonImage: "images/calendar.gif",
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			yearRange: ":+1"
		});
	});

	jQuery(document).ready(function(){
		$( "#patient_details" ).combogrid({
			url: 'modules.php?file=fetch_server&type=fetch&action=serachpatientbycasepaper',
			debug:true,
			//replaceNull: true,
			colModel: [{'columnName':'id','width':'10','label':'Id'}, {'columnName':'name','width':'60','label':'Full Name'},{'columnName':'case_paper_id','width':'30','label':'Case Paper Id'}],
			select: function( event, ui ) {
				$( "#patient_details" ).val( ui.item.name );
				$( "#patient_id" ).val( ui.item.id );
				$( "#case_paper_id" ).val( ui.item.case_paper_id );
				return false;
				}
		});
	});

	function searchPatient() {

		var intPatientId =	$('#patient_details').val();

		$strUrl = "modules.php?file=search_patient&type=search&patient_data=" + intPatientId;

		var intCasePaperId = '';

		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			success: function( returnData ) {
				strCustomerData = returnData;
		
				if( 'not found::' == strCustomerData ) {
					$( '#failed_display' ).css( "display", "block" );
					$( '#disp_failed' ).html( "Patient not found with :: " + $('#patient_details').val() );
					$( '#success_display' ).css( "display", "none" );
					$( '#patient_id' ).val( '' );
					$( '#case_paper_id' ).val( '' );

					emptyData();
				} else {
					$( '#failed_display' ).css( "display", "none" );
					$( '#success_display' ).css( "display", "block" );

					var strData = strCustomerData.split( '_' );
					
					$( '#disp_patient_name' ).html( "(" + strData[0] + ") " + strData[1] + " " + strData[2] );
					$( '#patient_id' ).val( strData[0] );
					$( '#case_paper_id' ).val( strData[3] );

					$('#view_yellow_pages').show();
					intCasePaperId = strData[3];

					loadYellowPageData( intCasePaperId );
					displayPatientInfo( strData[0] );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function loadYellowPageData( intCasePaperId ) {

		var intPatientId 	= $('#patient_details').val();
		var strAction 		= $( '#action' ).val();

		var postData = $('#patientAdmission').serializeArray();

		$strUrl = "modules.php?file=fetch_view_yellow_pages&type=fetch&action=" + strAction + "&patient_id=" + intPatientId + "&case_paper_id=" + intCasePaperId;
		
		$.ajax({
			url: $strUrl,
			type: "post",
			data: postData,
			beforeSend: function() {
				$("#wait").css("display","block");
				$('.col-lg-10').css("display","none");
			},
			success: function( returnData ) {
				strCustomerData = returnData;
				$('#view_yellow_pages' ). html( strCustomerData );
				$( "#view_yellow_pages" ).show( 300 );

				$("#wait").css("display","none");
				$('.col-lg-10').css("display","block");
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function displayPatientInfo( intPatientId ) {

		$strUrl = 'modules.php?file=patient_info&type=search&patient_id=' + intPatientId;
		
		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			success: function( returnData ) {
				strPatientData = returnData;
		
				if( 'not found::' == strPatientData ) {
					$( '#patient_info' ).html( '' );
					$( '#patient_info' ).css( "display", "none" );
				} else {
					$( '#patient_info' ).html( strPatientData );
					$( '#patient_info' ).css( "display", "block" );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	$( '#patient_details' ).keypress(function(e) {
		if( e.which == 13 ) {
			searchPatient();
			return false;
		}
	});
		
</script>
<style type="text/css">
	.ui-datepicker-year {
		background-color: #f6a828;
	}
	.ui-datepicker-month {
		background-color: #f6a828;
	}
	.text_area {
		resize:none;
	}
	
	.cg-menu {
		width: 400px;
	}
	
	.cg-menu-item {
		height: 34px;
		font-size: 17px;
	}
	.cg-comboButton {
		height: 50px;
		font-size: 20px;
	}
	
	.td_border_3_yellow {
		width: 30%;
		border-right: solid;
		border-bottom: solid;
		border-left: solid;
	}
	
	.td_border_middle_yellow {
		width: 30%;
		border-right: solid;
	}
</style>

</html>

<?php
	if( true == valObj( $objPatient, 'BasePatients' ) ) {
		echo "<script>searchPatient();</script>";
	}
?>